﻿<?php
require 'test_header.php';
require '../include/class/db_obj_user.class.php';

class test_user_register_process_class extends UnitTestCase{

	function setUp() {
		$db = db_connect();
		$sql = "
CREATE TABLE IF NOT EXISTS `test_user` (
  `username` varchar(255) NOT NULL,
  `passwd` varchar(128) NOT NULL,
  `role` varchar(16) NOT NULL,
  `state` varchar(16) NOT NULL,
  `register_time` varchar(64) DEFAULT NULL,
  `last_login_time` varchar(64) DEFAULT NULL,
  `last_login_ipadd` varchar(128) DEFAULT NULL,
  PRIMARY KEY (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8";
		$res = $db->query( $sql );
	
		$sql ="
CREATE TABLE IF NOT EXISTS `test_user_profile` (
  `username` varchar(255) NOT NULL,
  `nick` varchar(255) DEFAULT NULL,
  `email` varchar(255) DEFAULT NULL,
  `sex` varchar(6) DEFAULT NULL,
  `location` varchar(1024) DEFAULT NULL,
  `motto` varchar(1024) DEFAULT NULL,
  PRIMARY KEY (`user_name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8";
		$res = $db->query( $sql );

	}
	function tearDown(){ 
		
		$db = db_connect();
		$sql = 'DROP TABLE `test_user`';
		$res = $db->query( $sql );
		$sql = 'DROP TABLE `test_user_profile`' ;
		$res = $db->query( $sql );
	}

	function test_user_register_process_class_function_register(){

		$db = db_connect();
			
		$request = array( 'username' => rand() , 'passwd' => rand() );
	
		$user_register_process = new user_register_process( $db , $request );
		
		$this->assertTrue( $user_register_process->do_register() );

		$db_obj_user = new db_obj_user( db_connect() );
		//验证刚存入数据库中的用户是否可以被顺利读出
		$this->assertTrue( $db_obj_user->load( 'username' , $request['username'] ) );
		$this->assertEqual( $db_obj_user->get('passwd') , md5($request['passwd'] ) );

		//而未存入的用户读出是将会出错
		$this->assertFalse( $db_obj_user->load( 'username' , 'rand_is_cound`t_equal_me' ) );
	
	}

	function test_user_register_process_function_delete(){

		$db = db_connect();
		
		$request = array( 'username' => 'judas' , 'passwd' => '123' );
	
		$user_register_process = new user_register_process( $db , $request );
		
		$this->assertTrue( $user_register_process->do_register() );
		$this->assertTrue( user_register_process::delete_user( $db , $request['username'] ) );

		$db_obj_user = new db_obj_user( $db ) ;

		$this->assertFalse( $db_obj_user->load( 'username' , 'judas' ) );
	
	}


}

$test = new test_user_register_process_class();
$test->run( new HtmlReporter() );
